package com.dragon.client.controller.finance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.finance.IBillService;
import com.dragon.repository.domain.dto.BillDTO;
import com.dragon.repository.domain.dto.PlanCollectDTO;
import com.dragon.repository.domain.vo.BillVO;
import com.dragon.repository.entity.Bill;
import com.dragon.repository.entity.PlanCollect;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 账单表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-12-12
 */
@RestController
@RequestMapping("bill")
@Api(tags = "账单")
public class BillController {

    @Autowired
    IBillService service;

    @GetMapping("list")
    @ApiOperation(value = "账单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "customerId", value = "客户id", required = false, type = "int"),
            @ApiImplicitParam(name = "isFinish", value = "完成标识", required = false, type = "int"),
            @ApiImplicitParam(name = "time", value = "年月", required = false, type = "string")
    })
    public ResponseData<List<BillDTO>> billList(@RequestParam Integer page,
                                                @RequestParam Integer limit,
                                                @RequestParam(required = false) Integer customerId,
                                                @RequestParam(required = false) Integer isFinish,
                                                @RequestParam(required = false) String time){
        Page<Bill> billPage = service.billList(page, limit, customerId,  isFinish, time);
        return ResponseData.success(BillDTO.convertList(billPage.getRecords()), billPage.getTotal());
    }

    @GetMapping("planList")
    @ApiOperation(value = "获取应收款列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "customerId", value = "客户id", required = true, type = "int"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, type = "string"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, type = "string")
    })
    public ResponseData<List<PlanCollectDTO>> planList(Integer page, Integer limit, Integer customerId, String startTime, String endTime){
        Page<PlanCollect> planCollectPage = service.planCollects(page, limit, customerId, startTime, endTime);
        return ResponseData.success(PlanCollectDTO.convertList(planCollectPage.getRecords()), planCollectPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    private ResponseData billAdd(@RequestBody @Validated(Insert.class)BillVO vo){
        service.addBill(BillVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "更新")
    private ResponseData billUpdate(
            @ApiParam(name = "billId", value = "对账单id", required = true, type = "int")
            @RequestParam Integer billId){
        service.update(billId);
        return ResponseData.success();
    }

    @GetMapping("detailOne")
    @ApiOperation(value = "查看")
    private ResponseData<BillDTO> detailOne(
            @ApiParam(name = "billId", value = "对账单id", required = true, type = "int")
            @RequestParam Integer billId){
        Bill bill = service.detailOne(billId);
        return ResponseData.success(BillDTO.convert(bill));
    }

    @GetMapping(value = "export")
    @ApiOperation(value = "导出excel")
    public void exportExcel(
            @ApiParam(name = "id", value = "id", required = true, type = "int")
            @RequestParam Integer id,
            @ApiParam(hidden = true) HttpServletResponse response) throws UnsupportedEncodingException {
        service.export(id,response);
    }

    @GetMapping("statistics")
    @ApiOperation(value = "账单统计")
    public ResponseData<Map<String,Object>> billStatistics(
            @ApiParam(name = "customerId", value = "客户id", required = false, type = "int")
            @RequestParam(required = false) Integer customerId){
        Map<String,Object> map = service.billStatistics(customerId);
        return ResponseData.success(map);
    }

}
